package me.croute.calendarexample.asynctask;

import java.io.IOException;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.api.client.googleapis.extensions.android.gms.auth.GooglePlayServicesAvailabilityIOException;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;

import me.croute.calendarexample.activity.GVCalendarActivity;
import me.croute.calendarexample.activity.GVCalendarSettingsActivity;
import me.croute.googleapi.common.GoogleApiManager;
import android.app.Activity;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

abstract class GVCalendarAsyncTasks extends AsyncTask<Void, Void, Boolean> {
	// AsyncTask<String, Integer, Long>

	GVCalendarSettingsActivity gvSettings;
	// final CalendarModel model;
	final com.google.api.services.calendar.Calendar gvClient;

	public GVCalendarAsyncTasks(GVCalendarSettingsActivity activity) {
		// TODO Auto-generated constructor stub
		gvSettings = activity;
		gvClient = activity.getGvClient_Setting();
	}

	@Override
	protected void onPreExecute() {
		// Run on first by UI Thread AsynchTask
		super.onPreExecute();
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		try {
			doInBackground();
			return true;
		} catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
			gvSettings.showGooglePlayServicesAvailabilityErrorDialog(availabilityException.getConnectionStatusCode());
		} catch (UserRecoverableAuthIOException userRecoverableException) {
			gvSettings.startActivityForResult(userRecoverableException.getIntent(), GVCalendarSettingsActivity.REQUEST_AUTHORIZATION);
		} catch (IOException e) {
			logAndShow(gvSettings, GVCalendarActivity.TAG, e);
		}
		return false;

	}

	private void logAndShow(Activity activity, String tag, Throwable t) {
		Log.e(tag, "Error", t);
		String message = t.getMessage();
		if (t instanceof GoogleJsonResponseException) {
			GoogleJsonError details = ((GoogleJsonResponseException) t).getDetails();
			if (details != null) {
				message = details.getMessage();
			}
		} else if (t.getCause() instanceof GoogleAuthException) {
			message = ((GoogleAuthException) t.getCause()).getMessage();
		}
		showError(activity, message);

	}

	public void showError(Activity activity, String message) {
		String errorMessage = getErrorMessage(activity, message);
		showErrorInternal(activity, errorMessage);
	}

	private String getErrorMessage(Activity activity, String message) {
		Resources resources = activity.getResources();
		if (message == null) {
			// return resources.getString(R.string.error);
			return "Error is null";

		}
		// return resources.getString(R.string.error_format, message);
		return "Error";
	}

	private void showErrorInternal(final Activity activity, final String errorMessage) {
		activity.runOnUiThread(new Runnable() {
			public void run() {
				Toast.makeText(activity, errorMessage, Toast.LENGTH_LONG).show();
			}
		});
	}

	@Override
	protected void onProgressUpdate(Void... values) {
		// the callback method that display progress status.
		// onProgressUpdate(Integer... progress)
		super.onProgressUpdate(values);
	}

	@Override
	protected void onPostExecute(Boolean result) {
		// Auto run after onInBackground
		// onPostExecute(Long result)
		super.onPostExecute(result);
		if (result) {
			// Activity Refresh
		}
	}

	abstract protected void doInBackground() throws IOException;

}
